package it.polimi.traveldream.web.beans.administrator;

import it.polimi.traveldream.ejbclient.dtos.UserDTO;

import it.polimi.traveldream.ejbclient.eaos.AccountServiceEAO;
import it.polimi.traveldream.utils.exceptions.NotSuchUserException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="detailEmployee")
@RequestScoped
public class DetailEmployeeBean {
	@EJB
	private AccountServiceEAO aService;
	@ManagedProperty(value="#{listEmployees.selectedEmployee}")
	private UserDTO employee;
	
	public DetailEmployeeBean() {
	}

	public UserDTO getEmployee() {
		return employee;
	}

	public void setEmployee(UserDTO employee) {
		this.employee = employee;
	}

	public String deleteEmployee() {
		try {
			aService.unregisterUser(employee.getId());
			return "index";
		} catch (NotSuchUserException e) {
			FacesContextHandler.produceFacesMessage("growl", FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The selected employee cannot be reached.");
			return "detailEmployee";
		}
	}
	
}
